package cn.itsub.proxy.server.app.service.impl;

import cn.itsub.proxy.common.utils.Result;
import cn.itsub.proxy.server.app.entity.Tunnel;
import cn.itsub.proxy.server.app.mapper.TunnelMapper;
import cn.itsub.proxy.server.app.service.TunnelService;
import cn.itsub.proxy.server.app.utils.NetUtil;
import cn.itsub.proxy.server.work.config.ProxyConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

@Slf4j
@Service
public class TunnelServiceImpl implements TunnelService {

    @Autowired
    TunnelMapper tunnelMapper;

    @PostConstruct
    private void timer() {

    }
    private void init() {
        log.info("初始化数据");
        Map<String, List<Tunnel>> map = new HashMap<>();
        List<Tunnel> tunnels = null;//todo
        tunnels.forEach(t->{
            if (t.getState().equals(1)) {
                String clientKey = t.getClientKey();
                if (!map.containsKey(clientKey)) {
                    map.put(clientKey, new ArrayList<Tunnel>());
                }
                map.get(clientKey).add(t);
            }

        });
        map.entrySet().forEach(en->{
            String clientKey = en.getKey();
            ProxyConfig.getInstance().setClientTunnels(clientKey,en.getValue());
        });
    }

    @Override
    public int insertTunnel(String clientKey, int wanPort, String lanHost, int lanPort,String remark) {
        Tunnel tunnel = null;//todo
        if (tunnel!=null) {
            return 2; //外网端口已经存在
        }
        tunnel = new Tunnel();
        tunnel.setWanPort(wanPort);
        tunnel.setLanHost(lanHost);
        tunnel.setLanPort(lanPort);
        tunnel.setClientKey(clientKey);
        tunnel.setRemark(remark);
        tunnel.setState(0);
        //dao.saveTunnel(tunnel);
        return 1; //添加成功
    }

    @Override
    public void startTunnel(int tunnelId) {

    }

    @Override
    public void stopTunnel(int tunnelId) {

    }

    @Override
    public void deleteTunnel(int tunnelId) {

    }

    @Override
    public Result updateTunnel(Tunnel tunnel) {
        //数据校验
        if (!NetUtil.ipCheck(tunnel.getLanHost())) {
            return Result.of(91, "LanHost不是有效的IP地址");
        }
        if (tunnel.getWanPort()<50000||tunnel.getWanPort()>60000) {
            return Result.of(92, "外网端口超出范围");
        }
        if (tunnel.getLanPort()<0||tunnel.getLanPort()>65535) {
            return Result.of(92, "内网端口超出范围");
        }
        //是否已经登录

        //检查外网端口是否被占用
        /*var _tun = dao.getTunnelById(tunnel.getId());
        //修改（外网port，内网host，内网port，remark）
        _tun.setWanPort(tunnel.getWanPort());
        _tun.setLanHost(tunnel.getLanHost());
        _tun.setLanPort(tunnel.getLanPort());
        _tun.setRemark(tunnel.getRemark());
        storage.save();*/
        //todo

        //刷新到frp引擎
        this.frpReloadTunnels(tunnel.getClientKey());

        return Result.of(200, "修改成功");
    }

    public void frpReloadTunnels(String clientKey) {
        List<Tunnel> list = tunnelMapper.selectByClientKey(clientKey);
        ProxyConfig.getInstance().setClientTunnels(clientKey,list);
    }
}
